package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.TPatient;
import com.cookor.poi.modules.phy.service.TPatientService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tPatient"})
public class TPatientController extends BaseController {
  @Autowired
  private TPatientService tPatientService;
  
  @ModelAttribute
  public TPatient get(@RequestParam(required = false) String id) {
    TPatient entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tPatientService.get(id); 
    if (entity == null)
      entity = new TPatient(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tPatient:view"})
  @RequestMapping({"list", ""})
  public String list(TPatient tPatient, HttpServletRequest request, HttpServletResponse response, Model model) {
    Page<TPatient> page = this.tPatientService.findPage(new Page(request, response), tPatient);
    model.addAttribute("page", page);
    return "modules/phy/tPatientList";
  }
  
  @RequestMapping({"listselect"})
  public String listselect(TPatient tPatient, HttpServletRequest request, HttpServletResponse response, Model model) {
    Page<TPatient> page = this.tPatientService.findPage(new Page(request, response), tPatient);
    model.addAttribute("tPatient", tPatient);
    model.addAttribute("page", page);
    return "modules/phy/tPatientListSelect";
  }
  
  @RequiresPermissions({"phy:tPatient:view"})
  @RequestMapping({"form"})
  public String form(TPatient tPatient, Model model) {
    model.addAttribute("tPatient", tPatient);
    return "modules/phy/tPatientForm";
  }
  
  @RequiresPermissions({"phy:tPatient:edit"})
  @RequestMapping({"save"})
  public String save(TPatient tPatient, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, tPatient, new Class[0]))
      return form(tPatient, model); 
    this.tPatientService.save(tPatient);
    addMessage(redirectAttributes, new String[] { "保存就诊人成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tPatient/?repage";
  }
  
  @RequiresPermissions({"phy:tPatient:edit"})
  @RequestMapping({"delete"})
  public String delete(TPatient tPatient, RedirectAttributes redirectAttributes) {
    this.tPatientService.delete(tPatient);
    addMessage(redirectAttributes, new String[] { "删除就诊人成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tPatient/?repage";
  }
}
